package com.cuz.hot100;

public class Q58lengthOfLastWord {

    public static void main(String[] args) {
        System.out.println(lengthOfLastWord1("   fly me   to   the moon  "));
    }
    public static int lengthOfLastWord1(String s){
        if (s==null||s.length()==0){
            return 0;
        }
        char[] chars = s.toCharArray();
        int len=0;
        boolean hasLetter=false;
        for (int i = chars.length - 1; i >= 0; i--) {
            if (hasLetter){
                if (chars[i]==' '){
                    return len;
                }else {
                    len++;
                }
            }else {
                if (chars[i]==' '){
                    continue;
                }else {
                    hasLetter=true;
                    len++;
                }
            }
        }
        return len;
    }
}
